import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import '../../../../app_files.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(OrderDetailListViewState state, Dispatch dispatch,
    ViewService viewService) {
  final String desc = '对此订单有疑问，请咨询营业厅工作人员：${state.consumerPhone}';

  String getPayType(String typeID) {
    for (OrderPayTypeModel item in state.payTypeItem) {
      if (item.typeID == typeID) return item.typeName;
    }
    return '';
  }

  Widget _blueToothBindWidget() {
    /// 待圈存状态并且支持圈存的卡型才显示
    if (!state.model.isSupportSave) return Container();
    if (state.model.status != '1') return Container();
    if (state.bluetoothNo != null || state.model.isXABle) {
      return Container(
        margin: EdgeInsets.only(top: 10, left: 15, right: 15, bottom: 30),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(4)),
          color: ThemeConfig.mainColor,
        ),
        height: 50,
        width: double.infinity,
        child: MyInkWellWidget(
          child: Center(
            child: Padding(
              padding: EdgeInsets.only(bottom: 1),
              child: Text(
                '立即圈存',
                style: TextThemeConfig.Font16WithWhite,
              ),
            ),
          ),
          onTap: () => state.model.isXABle
              ? CommonUtils.pushRoute(
                  viewService.context, RoutePath.xaBleSaveView, arguments: {
                  'model': state.model,
                })
              : CommonUtils.pushRoute(
                  viewService.context, RoutePath.bluetoothSaveView,
                  arguments: {
                      'no': state.bluetoothNo,
                      'model': state.model,
                    }),
        ),
      );
    }
    return Padding(
      padding: EdgeInsets.only(bottom: 30),
      child: MyInkWellWidget(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset('images/ic_blue_tooth.png'),
            Padding(padding: EdgeInsets.only(left: 5)),
            Text(
              '绑定蓝牙读卡器',
              style: TextThemeConfig.Font13WithColorblue,
            ),
          ],
        ),
        onTap: () => CommonUtils.pushRoute(
            viewService.context, RoutePath.bindBlueToothView,
            arguments: {
              'userID': state.model.accountNo,
            }),
      ),
    );
  }

  List<Widget> _buildItem() {
    List _titles = [
      {'name': '缴费户名', 'value': state.model.name},
      {'name': '缴费户号', 'value': state.model.accountNo},
      {'name': '交易单号', 'value': state.model.payOrderNo},
      {'name': '交易时间', 'value': state.model.payTime},
      {'name': '交易方式', 'value': getPayType(state.model.payType)},
      {'name': '交易状态', 'value': state.model.orderStatus},
      {'name': '本次购气量', 'value': '${state.model.orderGas}m³'},
    ];
    if (state.model.orderRemain.length != 0)
      _titles.add(
        {'name': '本次结余', 'value': '¥${state.model.orderRemain}'},
      );
    if (state.model.lastRemain.length != 0)
      _titles.add(
        {'name': '上次结余', 'value': '¥${state.model.lastRemain}'},
      );

    _titles.add({'name': '气价信息', 'value': state.model.priceInfo});

    return _titles
        .map<Widget>(
          (e) => Container(
            padding: EdgeInsets.only(top: 4, bottom: 4),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  e['name'],
                  style: TextThemeConfig.Font14WithColor99,
                ),
                Expanded(
                  child: Container(
                    padding: EdgeInsets.only(left: 6),
                    child: Text(
                      e['value'],
                      style: TextThemeConfig.Font13WithColor22,
                      textAlign: TextAlign.right,
                    ),
                  ),
                ),
              ],
            ),
          ),
        )
        .toList();
  }

  return Scaffold(
    appBar: myAppBar(viewService.context, title: '购气记录'),
    body: state.payTypeItem == null
        ? MyLoadingWidget()
        : SingleChildScrollView(
            child: SafeArea(
              child: Container(
                width: double.infinity,
                child: Column(
                  children: [
                    Padding(
                      padding: EdgeInsets.only(top: 32),
                      child: Image.asset('images/icon_gas.png'),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 12),
                      child: Text(
                        '¥${state.model.money}',
                        style: TextStyle(
                          color: ThemeConfig.blackColor,
                          fontSize: 21,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 11),
                      child: Text(
                        '交易成功',
                        style: TextThemeConfig.Font15WithColor22,
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(
                          left: 15, right: 15, top: 30, bottom: 20),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(6)),
                      ),
                      padding: EdgeInsets.only(
                          top: 10, bottom: 10, left: 15, right: 17),
                      child: Column(
                        children: _buildItem(),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(bottom: 30, left: 15),
                      child: Text(
                        desc,
                        style: TextThemeConfig.Font12WithColor99,
                      ),
                    ),
                    _blueToothBindWidget(),
                    state.model.isXABle
                        ? Padding(
                            padding: EdgeInsets.only(bottom: 20),
                            child: MyInkWellWidget(
                              child: Text(
                                '设置上表模式',
                                style: TextThemeConfig.Font12WithMainColor,
                              ),
                              onTap: () => CommonUtils.pushRoute(
                                  viewService.context, RoutePath.xaBleSaveView,
                                  arguments: {
                                    'model': state.model,
                                    'isNetWork': true,
                                  }),
                            ),
                          )
                        : Container(),
                  ],
                ),
              ),
              top: false,
            ),
          ),
  );
}
